package com.pet.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.entity.PetActivity;
import com.pet.service.PetActivityService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (PetActivity)表控制层
 *
 * @author makejava
 * @since 2023-11-14 10:03:00
 */
@RestController
@RequestMapping("petActivity")
public class PetActivityController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private PetActivityService petActivityService;
    @Operation(summary = "查询活动信息 ")
    @GetMapping("/page")
    public R Page(@RequestParam Integer pageNum,
                  @RequestParam Integer pageSize,
                  @RequestParam(defaultValue = "") String name) {
        IPage<PetActivity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PetActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("is_delete = 0").like("activity_name", name);

        IPage<PetActivity> petActivityIPage = petActivityService.page(page, queryWrapper);

        return success(petActivityIPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "资产详情-用于批量删除")
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.petActivityService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param petActivity 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R save(@RequestBody PetActivity petActivity) {

        if(petActivity.getActivityId()==null){
            String dateFormat = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
            Date date = null;
            try {
                date = formatter.parse(DateUtil.now());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            petActivity.setActivityTime(date);
            //需要设置发布人
            petActivity.setUserId(1);
            petActivity.setIsDelete(0);
            System.out.println("petActivity"+petActivity);
            return success(this.petActivityService.save(petActivity));
        }else{
            return success(this.petActivityService.updateById(petActivity));
        }

    }

    /**
     * 修改数据
     *
     * @param petActivity 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody PetActivity petActivity) {
        return success(this.petActivityService.updateById(petActivity));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/del/batch")
    public R delete(@RequestBody List<Long> idList) {
        System.out.println(idList.toString());
        return success(this.petActivityService.removeByIds(idList));
    }

}

